/**
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 *
 *AppModuleIconController
 * 模块图标
 *
 * 修改纪录
 * 2023-01-13 版本：1.0 hudeyong 创建。
 * @version 版本：1.0
 * @author 作者：hudeyong
 * 创建日期：2023-01-13
 */

package com.topscomm.appv2.controller;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.topscomm.appv2.pojo.AppModuleIconEntity;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.appv2.controller.auto.AppModuleIconControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: hudeyong
 * @date: 2023-01-13
 * @description:模块图标Controller
 * @modify:
 */
@RestController
@RequestMapping("appv2/appModuleIcon")
public class AppModuleIconController extends AppModuleIconControllerAuto implements IAppModuleIconController {
	/**
	 * @author: hudeyong
	 * @date: 2023-01-13
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and modulename like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and modulecode like '{0}%' ", value);
		}
		return sql.toString();
	}

	@Override
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			int totalCount = this.service.getCount(sqlWhere);
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
						query.getSidx(), query.getSord());
			} else
				tempList = this.service.queryMapByWhere(sqlWhere);
			// this.service.setDispFields(tempList);
			for(Map<String, Object> temp : tempList){
				temp.put("moduleicon",this.service.queryModuleIcon(ConvertUtil.convertToString(temp.get("id"))));
			}
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @author: hudeyong
	 * @date: 2023-01-13
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: hudeyong
	 * @date: 2023-01-17
	 * @param paraMap
	 * @return
	 * @description:查询未配置图标模块
	 * @modify:
	 */
	@GetMapping(value = "/queryNoIconModule")
	public ResponseResult<Object> queryNoIconModule(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Query query = new Query(paraMap);
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			query.setPageSize(3000);
			List<Map<String, Object>> users = this.service.queryMapForPage(paraMap, "queryNoIconModule");
			Page page = new Page(users, users.size(), query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	@Override
	protected void beforeInsert(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeInsert(dataMap, paraMap);
		String moduleicon = ConvertUtil.convertToString(dataMap.get("moduleicon"));
		dataMap.put("moduleicon", moduleicon.getBytes(StandardCharsets.UTF_8));
	}

	@Override
	protected void beforeUpdate(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeUpdate(dataMap, paraMap);
		String moduleicon = ConvertUtil.convertToString(dataMap.get("moduleicon"));
		dataMap.put("moduleicon", moduleicon.getBytes(StandardCharsets.UTF_8));
	}

	@Override
	protected void afterLoad(Map<String, Object> paraMap) {
		super.afterLoad(paraMap);
		paraMap.put("moduleicon",this.service.queryModuleIcon(ConvertUtil.convertToString(paraMap.get("id"))));
	}

}

